package com.ops.service.ns.system.beans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSLookUps;
import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.OPSRuleRates;
import com.ops.schema.ns.system.OPSRules;
import com.ops.schema.ns.system.SearchLookUpsRequest;
import com.ops.schema.ns.system.SearchLookUpsResponse;
import com.ops.schema.ns.system.SearchRuleRatesRequest;
import com.ops.schema.ns.system.SearchRuleRatesResponse;
import com.ops.schema.ns.system.SearchRulesRequest;
import com.ops.schema.ns.system.SearchRulesResponse;
import com.ops.schema.ns.system.UpdateRulesRequest;
import com.ops.schema.ns.system.UpdateRulesResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class RuleMBean {

	private String ruleName;
	private String ruleType;
	private String ruleGroup;
	private String dataType;
	private String activeFrom;
	private String activeTo;
	private String mode;
	private String delflag;
	private String saveStatus;
	private List<OPSRules> opsrulelst;
	private OPSRules opsrule;
	private List<SelectItem> ruleTypeList;
	private List<SelectItem> dataTypeList;
	private List<SelectItem> formulaList;
	private List<SelectItem> rateList;
	private List<SelectItem> calcTypeList;
	private List<SelectItem> ruleGroupList;
	private List<SelectItem> levelList;
	private List<SelectItem> requiredList;
	private Map<String, OPSRules> opsruleMap;

	public void searchRules() {
		System.out.println("Inside searchRules");
		SearchRulesRequest rulesRequest = new SearchRulesRequest();
		rulesRequest.setDataType(dataType);
		rulesRequest.setRuleGroup(ruleGroup);
		rulesRequest.setRuleType(ruleType);
		rulesRequest.setRuleName(ruleName);
		rulesRequest.setActiveFrom(activeFrom);
		rulesRequest.setActiveTo(activeTo);
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		rulesRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchRulesResponse rulesResponse = OnlinePayrollSystemServiceClient
					.searchRule(rulesRequest);
			setOpsrulelst(rulesResponse.getOPSRules());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetRules() {
		setRuleName(null);
		setRuleType(null);
		setRuleGroup(null);
		setDataType(null);
		setActiveFrom(null);
		setActiveTo(null);
	}

	public void saveRules(ActionEvent event) {

		UpdateRulesRequest updateRequest = new UpdateRulesRequest();
		OPSRules opsRules = new OPSRules();
		opsRules.setCalcType(opsrule.getCalcType());
		opsRules.setDataType(opsrule.getDataType());
		opsRules.setDescription(opsrule.getDescription());
		opsRules.setFilter(opsrule.getFilter());
		opsRules.setFormula(opsrule.getFormula());
		opsRules.setId(opsrule.getId());
		opsRules.setLevel(opsrule.getLevel());
		opsRules.setParameters(opsrule.getParameters());
		opsRules.setRate(opsrule.getRate());
		opsRules.setRequired(opsrule.getRequired());
		opsRules.setRuleGroup(opsrule.getRuleGroup());
		opsRules.setRuleName(opsrule.getRuleName());
		opsRules.setRuleType(opsrule.getRuleType());
		opsRules.setValueFormat(opsrule.getValueFormat());
		opsRules.setOperationFlag("U");
		opsRules.setActiveFrom(opsrule.getActiveFrom());
		opsRules.setActiveTo(opsrule.getActiveTo());
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		if (opsRules.getId() != null)
			uditInfo.setAction("U");
		else
			uditInfo.setAction("I");
		uditInfo.setObjectType(getMenuId());
		opsRules.setOPSRecordAuditInfo(uditInfo);
		updateRequest.getOPSRules().add(opsRules);
		try {
			UpdateRulesResponse response = persistData(updateRequest);
			System.out.println("STATUS:"
					+ response.getServiceResult().getStatusMsg());
			setSaveStatus(response.getServiceResult().getStatusMsg());
			searchRules();
		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void delRules(ActionEvent event) {
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (mode.startsWith("Del_") && delflag != null
					&& delflag.equalsIgnoreCase("Yes")) {
				setDelflag(null);
				UpdateRulesRequest updateRequest = new UpdateRulesRequest();
				String key = mode.replace("Del_", "").trim();
				OPSRules rules = new OPSRules();
				rules.setId(key);
				rules.setOperationFlag("D");
				OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
				uditInfo.setUpdatedBy(getuserId());
				uditInfo.setAction("D");
				uditInfo.setObjectType(getMenuId());
				uditInfo.setUpdatedSession(getSessionId());
				rules.setOPSRecordAuditInfo(uditInfo);
				updateRequest.getOPSRules().add(rules);
				try {
					persistData(updateRequest);
					searchRules();
				} catch (ServiceException e) {
					e.printStackTrace();
				}

			}
		}

	}

	public OPSRules addRules() {
		setSaveStatus(null);
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (getOpsrulelst() != null) {
				opsruleMap = new HashMap<String, OPSRules>();
				for (OPSRules rules : getOpsrulelst()) {
					System.out.println("Inside" + rules.getId());
					opsruleMap.put(rules.getId(), rules);
				}
			}
			if (mode.equalsIgnoreCase("newAdd")) {
				opsrule = new OPSRules();
			} else if (mode.startsWith("Edit_")) {
				String key = mode.replace("Edit_", "").trim();
				System.out.println("Key:" + key);
				opsrule = opsruleMap.get(key);
			}
		}
		System.out.println("Inside addRules:::" + mode);
		return opsrule;
	}

	public UpdateRulesResponse persistData(UpdateRulesRequest rulesRequest)
			throws ServiceException {
		System.out.println();
		UpdateRulesResponse rulesResponse = OnlinePayrollSystemServiceClient
				.saveRule(rulesRequest);
		return rulesResponse;
	}

	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext()
				.getSession(false);
		return session.getId();
	}

	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}

	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}

	public String getRuleName() {
		return ruleName;
	}

	public void setRuleName(String ruleName) {
		this.ruleName = ruleName;
	}

	public String getRuleType() {
		return ruleType;
	}

	public void setRuleType(String ruleType) {
		this.ruleType = ruleType;
	}

	public String getRuleGroup() {
		return ruleGroup;
	}

	public void setRuleGroup(String ruleGroup) {
		this.ruleGroup = ruleGroup;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getActiveFrom() {
		return activeFrom;
	}

	public void setActiveFrom(String activeFrom) {
		this.activeFrom = activeFrom;
	}

	public String getActiveTo() {
		return activeTo;
	}

	public void setActiveTo(String activeTo) {
		this.activeTo = activeTo;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getDelflag() {
		return delflag;
	}

	public void setDelflag(String delflag) {
		this.delflag = delflag;
	}

	public String getSaveStatus() {
		return saveStatus;
	}

	public void setSaveStatus(String saveStatus) {
		this.saveStatus = saveStatus;
	}

	public List<OPSRules> getOpsrulelst() {
		return opsrulelst;
	}

	public void setOpsrulelst(List<OPSRules> opsrulelst) {
		this.opsrulelst = opsrulelst;
	}

	public OPSRules getOpsrule() {
		return opsrule;
	}

	public void setOpsrule(OPSRules opsrule) {
		this.opsrule = opsrule;
	}

	public List<SelectItem> getRuleTypeList() {
		if (ruleTypeList == null) {
			ruleTypeList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			ruleTypeList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("RULE-TYPE");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					ruleTypeList.add(item);
				}
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return ruleTypeList;
	}

	public void setRuleTypeList(List<SelectItem> ruleTypeList) {
		this.ruleTypeList = ruleTypeList;
	}

	public List<SelectItem> getDataTypeList() {
		if (dataTypeList == null) {
			dataTypeList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			dataTypeList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("DATA-TYPE");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					dataTypeList.add(item);
				}
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return dataTypeList;
	}

	public void setDataTypeList(List<SelectItem> dataTypeList) {
		this.dataTypeList = dataTypeList;
	}

	public List<SelectItem> getCalcTypeList() {
		if (calcTypeList == null) {
			calcTypeList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			calcTypeList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("CALC-TYPE");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					calcTypeList.add(item);
				}
				System.out.println("calcTypeList:" + calcTypeList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return calcTypeList;
	}

	public void setCalcTypeList(List<SelectItem> calcTypeList) {
		this.calcTypeList = calcTypeList;
	}

	public List<SelectItem> getRuleGroupList() {
		if (ruleGroupList == null) {
			ruleGroupList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			ruleGroupList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			masterRequest.setType("RULE-GROUP");
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					ruleGroupList.add(item);
				}
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return ruleGroupList;
	}

	public void setRuleGroupList(List<SelectItem> ruleGroupList) {
		this.ruleGroupList = ruleGroupList;
	}

	public List<SelectItem> getLevelList() {
		if (levelList == null) {
			levelList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			levelList.add(defitem);
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("LEVEL");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					levelList.add(item);
				}
				System.out.println("Level:" + levelList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return levelList;
	}

	public void setLevelList(List<SelectItem> levelList) {
		this.levelList = levelList;
	}

	public Map<String, OPSRules> getOpsruleMap() {
		return opsruleMap;
	}

	public void setOpsruleMap(Map<String, OPSRules> opsruleMap) {
		this.opsruleMap = opsruleMap;
	}

	public List<SelectItem> getRequiredList() {
		if (requiredList == null) {
			requiredList = new ArrayList<SelectItem>();
			SearchLookUpsRequest masterRequest = new SearchLookUpsRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			masterRequest.setType("BOOLEAN-TYPE");
			try {
				SearchLookUpsResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest);
				for (OPSLookUps master : masterResponse.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					requiredList.add(item);
				}
				System.out.println("requiredList:" + requiredList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return requiredList;
	}

	public void setRequiredList(List<SelectItem> requiredList) {
		this.requiredList = requiredList;
	}

	public List<SelectItem> getFormulaList() {
		if (formulaList == null) {
			formulaList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			formulaList.add(defitem);
			try {
				SearchLookUpsRequest masterRequest_system = new SearchLookUpsRequest();
				OPSRecordAuditInfo uditInfo_system = new OPSRecordAuditInfo();
				masterRequest_system.setOPSRecordAuditInfo(uditInfo_system);
				masterRequest_system.setType("FORMULA-SYSTEM");
				SearchLookUpsResponse masterResponse_system = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest_system);
				for (OPSLookUps master : masterResponse_system.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					formulaList.add(item);
				}
				SearchLookUpsRequest masterRequest_user = new SearchLookUpsRequest();
				OPSRecordAuditInfo uditInfo_user = new OPSRecordAuditInfo();
				masterRequest_user.setOPSRecordAuditInfo(uditInfo_user);
				masterRequest_user.setType("FORMULA-USER");
				SearchLookUpsResponse masterResponse_user = OnlinePayrollSystemServiceClient
						.searchLookUp(masterRequest_user);
				for (OPSLookUps master : masterResponse_user.getOPSLookUps()) {
					SelectItem item = new SelectItem(master.getCode(),
							master.getName());
					formulaList.add(item);
				}
				System.out.println("formulaList:" + formulaList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return formulaList;
	}

	public void setFormulaList(List<SelectItem> formulaList) {
		this.formulaList = formulaList;
	}

	public List<SelectItem> getRateList() {
		if (rateList == null) {
			rateList = new ArrayList<SelectItem>();
			SelectItem defitem = new SelectItem("", "--Select--");
			rateList.add(defitem);
			SearchRuleRatesRequest masterRequest = new SearchRuleRatesRequest();
			OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
			masterRequest.setOPSRecordAuditInfo(uditInfo);
			try {
				SearchRuleRatesResponse masterResponse = OnlinePayrollSystemServiceClient
						.searchRuleRate(masterRequest);
				for (OPSRuleRates master : masterResponse.getOPSRuleRates()) {
					SelectItem item = new SelectItem(master.getRateId(),
							master.getRateId());
					rateList.add(item);
				}
				System.out.println("rateList:" + rateList.size());
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		}
		return rateList;
	}

	public void setRateList(List<SelectItem> rateList) {
		this.rateList = rateList;
	}

}
